package com.singfan.core.util;

import com.singfan.common.util.StringUtil;
import com.singfan.core.dao.database.model.constant.GlobalConfig;

import java.util.UUID;

/**
 * Author: hansonghe
 * Date: 2015-08-21 14:33
 * Desc:
 */
public class AccessTokenUtil {
    private static final String CUSTOMER_ACCESS_TOKEN_SEPARATOR = "-1-";
    private static final String BARBER_ACCESS_TOKEN_SEPARATOR = "-2-";
    private static final String SHOP_ACCESS_TOKEN_SEPARATOR = "-3-";

    /**
     *
     * @param id
     * @return
     */
    public static String createCustomerAccessToken(long id){
        return id + CUSTOMER_ACCESS_TOKEN_SEPARATOR + UUID.randomUUID().toString().substring(0,8);
    }

    /**
     *
     * @param id
     * @return
     */
    public static String createBarberAccessToken(long id){
        return id + BARBER_ACCESS_TOKEN_SEPARATOR + UUID.randomUUID().toString().substring(0,8);
    }

    /**
     *
     * @param id
     * @return
     */
    public static String createShopAccessToken(long id){
        return id + SHOP_ACCESS_TOKEN_SEPARATOR + UUID.randomUUID().toString().substring(0,8);
    }

    public static boolean isCustomerAccessToken(String accessToken){
        boolean result = false;

        if (!StringUtil.isNullOrEmpty(accessToken)){
            result = accessToken.contains(CUSTOMER_ACCESS_TOKEN_SEPARATOR);
        }

        return result;
    }

    public static boolean isBarberAccessToken(String accessToken){
        boolean result = false;

        if (!StringUtil.isNullOrEmpty(accessToken)){
            result = accessToken.contains(BARBER_ACCESS_TOKEN_SEPARATOR);
        }

        return result;
    }

    public static boolean isShopAccessToken(String accessToken){
        boolean result = false;

        if (!StringUtil.isNullOrEmpty(accessToken)){
            result = accessToken.contains(SHOP_ACCESS_TOKEN_SEPARATOR);
        }

        return result;
    }


    /**
     *
     * @param tokenCreateTime
     * @return
     */
    public static boolean isExpired(long tokenCreateTime){
        boolean result = true;

        if (System.currentTimeMillis() - tokenCreateTime <= GlobalConfig.CUSTOMER_ACCESS_TOKEN_EXPIRED){
            result = false;
        }

        return result;
    }
}
